from sqlalchemy import Column, Integer, String, Date

from database import Base


class Medicine(Base):
    __tablename__ = "medicine"

    ID = Column(Integer, primary_key=True, index=True, autoincrement=True)
    medicine_name = Column(String(50))
    main_indications=Column(String(50))
    stock=Column(Integer)
    batch=Column(Integer)
    expiry_date=Column(Date)    #使用Date类型
    supplier=Column(String(50))
    price=Column(Integer)
    unit=Column(String(50))

    def to_dict(self):
        return {
            "ID": self.ID,
            "medicine_name": self.name,
            "stock": self.stock,
            "batch": self.batch,
            "expiry_date": self.expiry_date,
            "supplier": self.supplier,
            "price": self.price,
            "unit": self.unit,
        }

    @classmethod
    def from_dict(cls, data: dict) -> "Medicine":
        valid_keys = set(cls.__table__.columns.keys())
        return cls(**{k: v for k, v in data.items() if k in valid_keys})
